package com.xbjtech.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xbjtech.model.MobileYzm;
import com.xbjtech.service.MobileYzmService;
import com.xbjtech.service.SendSmsService;
import com.xbjtech.shiro.MobileYzmToken;
import com.xbjtech.util.StringUtils;

import lombok.extern.slf4j.Slf4j;

@Controller
@Slf4j
public class LoginController {
	
	@Autowired
	MobileYzmService mobileYzmService;
	
	@Autowired
	SendSmsService sendSmsService;
	

	@GetMapping("/login")
	public String toLogin() {
		return "login";
	}

	@PostMapping("/login")
	public String login(String username, String password, Model model) {
		Subject subject = SecurityUtils.getSubject();
		if (!subject.isAuthenticated()) {
			UsernamePasswordToken token = new UsernamePasswordToken(username, password);
			token.setRememberMe(true);
			try {
				log.info("token:{}", token.hashCode());
				subject.login(token);
			} catch (UnknownAccountException uae) {
				log.info("There is no user with username of " + token.getPrincipal());
				model.addAttribute("errorMessage", uae.getMessage());
				return "login";
			} catch (IncorrectCredentialsException ice) {
				log.info("Password for account " + token.getPrincipal() + " was incorrect!");
				model.addAttribute("errorMessage", "用户名密码不匹配");
				return "login";
			} catch (AuthenticationException ae) {
				model.addAttribute("errorMessage", "登录失败");
				return "login";
			}
		}
		return "redirect:/main";
	}
	
	@PostMapping("/phonelogin")
	public String phoneLogin(String mobile, String yzm, Model model, HttpServletRequest request) {
		log.info("手机号:{}--->验证码:{}", mobile, yzm);
		Subject subject = SecurityUtils.getSubject();
		if (!subject.isAuthenticated()) {
			MobileYzmToken token = new MobileYzmToken(mobile, yzm, request.getRemoteHost());
			try {
				subject.login(token);
			} catch (UnknownAccountException uae) {
				model.addAttribute("errorMessage", uae.getMessage());
				return "login";
			} catch (IncorrectCredentialsException ice) {
				model.addAttribute("errorMessage", "验证码不匹配");
				return "login";
			} catch (AuthenticationException ae) {
				model.addAttribute("errorMessage", "登录失败");
				return "login";
			}
		}
		return "redirect:/main";
	}
	
	@GetMapping("/sendyzm")
	@ResponseBody
	public Map<String, Boolean> sendMsg(String mobile) {
		
		// 1.产生随机的验证码
		String yzm = StringUtils.randomYzm(6);
		// 2.产生随机的验证码要跟手机号进行关联
		MobileYzm my = new MobileYzm();
		my.setMobile(mobile);
		my.setYzm(yzm);
		mobileYzmService.save(my);
		
		// 3.发短信
		boolean flag = sendSmsService.sendLoginYzm(mobile, yzm);
		
		// 4.返回短信发送状态
		Map<String, Boolean> result = new HashMap<>();
		result.put("success", flag);
		
		return result;
	}

	@RequestMapping("/main")
	public String main() {
		return "main";
	}

}
